library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.0     ✓ purrr   0.3.3
## ✓ tibble  3.0.0     ✓ dplyr   1.0.2
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ───────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
library(here)
## here() starts at /Users/sarinasinghkhaira/health_group_project/r_dashboard
#read in shapefile
scot_la <- st_read(here::here("clean_data/scot_la.shp"))
## Reading layer `scot_la' from data source `/Users/sarinasinghkhaira/health_group_project/r_dashboard/clean_data/scot_la.shp' using driver `ESRI Shapefile'
## Simple feature collection with 32 features and 16 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 5547.001 ymin: 530252.9 xmax: 470319.5 ymax: 1220298
## CRS:            27700
#read in mental health data
all_time_mental <- read_csv(here::here("clean_data/all_time_mental.csv"))
## Warning: Missing column names filled in: 'X1' [1]
## Parsed with column specification:
## cols(
##   X1 = col_double(),
##   feature_code = col_character(),
##   date_code = col_character(),
##   swem_score = col_double(),
##   type_of_tenure = col_character(),
##   household_type = col_character(),
##   age = col_character(),
##   gender = col_character(),
##   limiting_long_term_physical_or_mental_health_condition = col_character(),
##   la_name = col_character()
## )
#read in simd data 
simd <- read_csv(here::here("clean_data/simd_la_2012_2020.csv")) %>%
  filter(year == "2016")
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   la_code = col_character(),
##   la_name = col_character()
## )
## See spec(...) for full column specifications.
#join simd to mental health data


#Join mental health data to shapefile
scot_la <- scot_la %>%
  left_join(all_time_mental, by = c("area_cod_1" = "feature_code"))

#Check projection, for use with leaflet must be WGS84
#st_crs(scot_la)

#Transform projection from Transverse Mercator to WGS84
transformed <- st_transform(scot_la, '+proj=longlat +datum=WGS84')
transformed %>% 
ggplot(aes(fill = swem_score)) + 
  geom_sf() +
  theme_minimal() +
  scale_fill_viridis_c(option = "plasma")

library(leaflet)
# Set pallete for SWEM score
pal <- colorNumeric(
  palette = "YlGnBu",
  domain = transformed$swem_score
)

bbox <- st_bbox(transformed) %>% 
  as.vector()

leaflet(transformed) %>% 
  #add base tiles
  addProviderTiles("CartoDB.Positron") %>% 
  # add swem score layer
  addPolygons(fillColor = ~pal(swem_score),
              color = "black",
              weight = 0.1,
              smoothFactor = 0.9,
              opacity = 0.8,
              fillOpacity = 0.9,
              highlightOptions = highlightOptions(color = "white",
                                                  weight = 1,
                                                  bringToFront = TRUE)) %>%
  #add legend
  addLegend("bottomright", pal = pal, values = ~swem_score,
    title = "Average SWEM",
    opacity = 1
  ) %>% 
  fitBounds(bbox[1], bbox[2], bbox[3], bbox[4])